package com.fuyao.cloud.personnel.library.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fuyao.cloud.common.biz.dto.PageQuery;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.library.entity.ApplyBook;
import com.fuyao.cloud.personnel.library.entity.Book;
import com.fuyao.cloud.personnel.library.entity.BookCategory;
import com.fuyao.cloud.personnel.library.service.ApplyBookService;
import com.fuyao.cloud.personnel.library.service.BookCategoryService;
import com.fuyao.cloud.personnel.library.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/book")
@Api(value = "book", tags = "书籍信息管理")
public class BookController {

    private final BookService bookService;
    private final BookCategoryService bookCategoryService;
    private final ApplyBookService applyBookService;

    /**
     * 分页查询
     *
     * @param pageQuery 分页对象
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page")
//    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<IPage<Book>> getPage(PageQuery<Book> pageQuery) {
        return R.ok(bookService.selectBookPage(pageQuery.getPage(), pageQuery.getExample()));
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Book> getById(@PathVariable("id") Long id) {
        Book book = bookService.getById(id);
        if (book.getCatalogId() != null) {
            BookCategory bookCategory = bookCategoryService.getById(book.getCatalogId());
            book.setCatalogCode(bookCategory.getFullName());
        }

        ApplyBook applyBook = new ApplyBook();
        applyBook.setBookId(book.getId());
        long count = applyBookService.count(Wrappers.<ApplyBook>lambdaQuery().eq(ApplyBook::getBookId,book.getId()));
        book.setHasBorrowHistory(count > 0);

        return R.ok(book);
    }

    /**
     * 新增
     *
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Book> save(@RequestBody Book book) {
        bookService.save(book);
        return R.ok(book);
    }

    /**
     * 修改
     *
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> updateById(@RequestBody Book book) {
        return R.ok(bookService.updateById(book));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(bookService.removeById(id));
    }


    /**
     * 导出 excel 表格
     *
     * @param book 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public List<Book> export(Book book) {
        return bookService.list(Wrappers.query(book));
    }

    /**
     * 创建方法
     */
    @GetMapping(value = "/create")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Book> create() {
        return R.ok(bookService.create());
    }

    @PostMapping(value = "/importData")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        return R.ok(bookService.importData(file));
    }

    /**
     * 批量删除方法
     *
     * @return R
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @SysLog("批量删除")
    @DeleteMapping("/batchDelete")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return R.ok(bookService.removeByIds(ids));
    }
}
